package com.ysd.lis.service.qi.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.LabSampleType;
import com.ysd.lis.entity.qi.QiSampleTypeCompare;
import com.ysd.lis.mapper.lab.LabSampleTypeMapper;
import com.ysd.lis.mapper.qi.QiSampleTypeCompareMapper;
import com.ysd.lis.request.DeleteParam;
import com.ysd.lis.request.qi.SampleTypeCompareParam;
import com.ysd.lis.service.qi.QiSampleTypeCompareService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 任务与指标项目对照表 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-08-06
 */
@Service
public class QiSampleTypeCompareServiceImpl extends ServiceImpl<QiSampleTypeCompareMapper, QiSampleTypeCompare> implements QiSampleTypeCompareService {

    @Autowired
    QiSampleTypeCompareMapper qiSampleTypeCompareMapper;
    @Autowired
    LabSampleTypeMapper labSampleTypeMapper;
    @Override
    public Result findLabSampleTypeList(SampleTypeCompareParam param) {
        MPJLambdaWrapper<LabSampleType> lambdaQueryWrapper = new MPJLambdaWrapper();
        lambdaQueryWrapper.selectAll(LabSampleType.class)
                .eq(LabSampleType::getDelFlag, 0);
        if(ToolsUtils.isNotEmpty(param.getSeachValue())){
            lambdaQueryWrapper.and(wrapper ->
                    wrapper.like(LabSampleType::getSampleTypeCode, param.getSeachValue())
                            .or()
                            .like(LabSampleType::getSampleTypeName, param.getSeachValue())
                            .or()
                            .like(LabSampleType::getInputCode, param.getSeachValue())
            );
        }
//        String qiSampleTypeCode = param.getQiSampleTypeCode();
//        if(ToolsUtils.isNotEmpty(qiSampleTypeCode)){
//            StringBuffer existsSql = new StringBuffer("SELECT 1 FROM QI_SAMPLE_TYPE_COMPARE t2 WHERE t.SAMPLE_TYPE_CODE = t2.SAMPLE_TYPE_CODE AND t2. QI_SAMPLE_TYPE_CODE = '" + qiSampleTypeCode + "' ");
//            lambdaQueryWrapper.notExists(existsSql.toString());
//        }
        StringBuffer existsSql = new StringBuffer("SELECT 1 FROM QI_SAMPLE_TYPE_COMPARE t2 WHERE t.SAMPLE_TYPE_CODE = t2.SAMPLE_TYPE_CODE");
        lambdaQueryWrapper.notExists(existsSql.toString());
        List<LabSampleType> labSampleTypes = labSampleTypeMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", labSampleTypes);

    }

    @Override
    public Result addSampleTypeCompare(QiSampleTypeCompare qiSampleTypeCompare) {
        if (ToolsUtils.isEmpty(qiSampleTypeCompare)) {
            Result.fail("入参不可为空，入参异常");
        }
        boolean save = this.save(qiSampleTypeCompare);
        if (save) {
            return Result.succ(1, "", null);
        } else {
            return Result.fail("添加失败！");
        }
    }

    @Override
    public Result findSampleTypeCompareList(QiSampleTypeCompare qiSampleTypeCompare) {
        MPJLambdaWrapper<QiSampleTypeCompare> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper.selectAll(QiSampleTypeCompare.class)
                .selectAs(LabSampleType::getSampleTypeName, QiSampleTypeCompare::getSampleTypeName)
                .selectAs(LabSampleType::getInputCode, QiSampleTypeCompare::getInputCode)
                .leftJoin(LabSampleType.class, p->p.eq(LabSampleType::getSampleTypeCode,QiSampleTypeCompare::getSampleTypeCode).eq(LabSampleType::getDelFlag,0))
                .eq(QiSampleTypeCompare::getQiSampleTypeCode, qiSampleTypeCompare.getQiSampleTypeCode())
                .eq(QiSampleTypeCompare::getDelFlag, 0)
                .orderByAsc(QiSampleTypeCompare::getSeq);
        List<QiSampleTypeCompare> qiSampleTypeCompares = qiSampleTypeCompareMapper.selectList(mpjLambdaWrapper);
        return Result.succ(qiSampleTypeCompares);
    }

    @Override
    public Result deleteSampleTypeCompare(DeleteParam deleteParam) {
        int i = qiSampleTypeCompareMapper.deleteById(deleteParam.getId());
        if (i > 0) {
            return Result.succ(1, "移除成功", null);
        } else {
            return Result.fail("移除失败！");
        }
    }
}
